﻿namespace K9Nano.Utils;

public static class MimeUtils
{
    /// <summary>
    /// 根据文件扩展名获取常用的ContentType
    /// </summary>
    /// <param name="ext">文件扩展名</param>
    /// <returns>对应的ContentType，如果未找到则返回"application/octet-stream"</returns>
    public static string GetContentType(string ext)
    {
        if (string.IsNullOrWhiteSpace(ext))
            return "application/octet-stream";

        ext = ext.ToLowerInvariant().TrimStart('.');

        return ext switch
        {
            "txt" => "text/plain",
            "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            "xls" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            "html" => "text/html",
            "htm" => "text/html",
            "css" => "text/css",
            "js" => "application/javascript",
            "json" => "application/json",
            "xml" => "application/xml",
            "jpg" => "image/jpeg",
            "jpeg" => "image/jpeg",
            "png" => "image/png",
            "gif" => "image/gif",
            "bmp" => "image/bmp",
            "webp" => "image/webp",
            "svg" => "image/svg+xml",
            "pdf" => "application/pdf",
            "zip" => "application/zip",
            "rar" => "application/vnd.rar",
            "7z" => "application/x-7z-compressed",
            "mp3" => "audio/mpeg",
            "wav" => "audio/wav",
            "mp4" => "video/mp4",
            "avi" => "video/x-msvideo",
            "mov" => "video/quicktime",
            _ => "application/octet-stream"
        };
    }
}
